package com.customize.application;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import javax.servlet.ServletException;
import java.io.File;

/**
 * @Author ZhangGJ
 * @Date 2021/05/13 08:00
 */
public class TomcatConfiguration {

    private static final int SERVER_PORT = 8089;

    public void start() throws LifecycleException, ServletException {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(SERVER_PORT);
        StandardContext standardContext =
                (StandardContext) tomcat.addWebapp("/", new File("src/main").getAbsolutePath());
        standardContext.setReloadable(false);
        File additionWebInfClasses = new File("target/classes");
        WebResourceRoot resources = new StandardRoot(standardContext);
        resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes",
                additionWebInfClasses.getAbsolutePath(), "/"));
        tomcat.start();
        System.out.println("tomcat启动成功...");
        tomcat.getServer().await();
    }

}
